<template></template>

<script>
import { selectDiserRuleDetail } from "@/api/datascan/scanresults/table";
import { _deepClone } from "@/utils";

export default {
  name: "index",
  data() {
    return {
      ids: [],
      chooseColumnName: [],
      form: {
        chooseIds: [],
      },
      nums: 2,
      tempList: [],
      keyIndex: 0,
    };
  },
  methods: {
    moreTool(operate) {
      let nums = operate === "two" ? 2 : 5;
      if (this.nums !== nums) {
        this.initDataByNums(nums);
      }
      this.nums = nums;
    },
    /**
     * 数据初始化
     */
    initDataByNums(nums) {
      if (this.type === "column") {
        this.handleSetDataByColumn(nums);
      } else {
        this.handleSetDataByTable(nums);
      }
    },
    /**
     * 参数传值数据进行处理
     */
    handleSetDataByColumn(nums) {
      this.loading = true;
      let sample = this.info?.remark.split(",") || [],
        other = [];
      if (sample && sample.length < nums) {
        for (let i = 0; i < nums - sample.length; i++) {
          other.push("");
        }
      } else if (sample && sample.length > nums) {
        sample.splice(nums, sample.length - nums);
      }
      this.list = [
        {
          columnName: this.info.columnName,
          columnRemark: this.info.columnRemark,
          remark: [...sample, ...other],
          typeIds: this.info[this.key]?.split(",") || [],
        },
      ];

      this.tempList = _deepClone(this.list);
      this.loading = false;
    },
    /**
     * 接口请求数据进行处理
     */
    handleSetDataByTable(nums) {
      this.getSelectedData(nums);
    },

    getSelectedData(nums) {
      this.loading = true;
      selectDiserRuleDetail({ id: this.info.id })
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.data.reduce((r, c) => {
              let sample = c.remark?.split(",") || ["", ""],
                other = [];
              if (sample && sample.length < nums) {
                for (let i = 0; i < nums - sample.length; i++) {
                  other.push("");
                }
              } else if (sample && sample.length > nums) {
                sample.splice(nums, sample.length - nums);
              }
              r.push({
                scanResultId: c.scanResultId,
                columnName: c.columnName,
                columnRemark: c.columnRemark,
                remark: [...sample, ...other],
                typeIds: c[this.key]?.split(",") || [],
              });
              return r;
            }, []);

            this.tempList = _deepClone(this.list);
          }
        })
        .finally(() => (this.loading = false));
    },

    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map((item) => item.scanResultId);
      this.chooseColumnName = selection.map((item) => item.columnName);
    },

    handleData() {
      let typeIds = [],
        list = [];
      this.list.map((item, index) => {
        if (item.typeIds.length) {
          typeIds.push(item.typeIds);
        }
        list.push({
          id: item?.scanResultId,
          scanResultId: item?.scanResultId,
          columnName: item.columnName,
          typeIds: item.typeIds.join(","),
          ruleIds: item.typeIds.join(","),
          flag:
            item.typeIds.join(",") !== this.tempList[index].typeIds.join(",")
              ? 1
              : null,
        });
      });
      return {
        typeIds,
        list,
      };
    },

    /**
     * 打开批量设置弹窗
     */
    handleSet() {
      this.open = true;
    },
    /**
     * 关闭批量设置弹窗
     */
    cancel() {
      this.form.chooseIds = [];
      this.open = false;
    },

    submitForm() {
      this.buttonLoading = true;
      this.list.forEach((item) => {
        if (this.ids.includes(item.scanResultId)) {
          item.typeIds = this.form.chooseIds;
          item.ruleIds = this.form.chooseIds;
        }
      });

      this.buttonLoading = false;
      this.ids = [];
      this.chooseColumnName = [];
      this.cancel();
      this.keyIndex++;
    },

    changeType(v) {},

    handleClose(index) {
      this.chooseColumnName.splice(index, 1);
      this.ids.splice(index, 1);
    },
  },
};
</script>

<style scoped></style>
